<template>
  <div class="fenxiao-goods">
    <dls-header :type="0"
                :Data="fxsHeaderData"></dls-header>
    <div class="item-card">
      <search-box @search="search"
                  v-model="keyWord"
                  :placeholder="Placeholder"
                  :showBtn="showBtn"></search-box>
      <form action
            v-on:submit.prevent>
        <div class="select-item">
          <div class="all-class"
               @click="clickAllClass">
            <span class="text">{{allClassVal}}</span>
            <i class="icon-right"></i>
          </div>
          <!-- <div class="text">价格区间：</div> -->
          <div class="price">
            <input type="text"
                   @keyup.enter="search"
                   v-model="startPrice"
                   placeholder="开始价格" />
          </div>
          <div class="price">
            <input type="text"
                   @keyup.enter="search"
                   v-model="endPrice"
                   placeholder="结束价格" />
          </div>
          <div class="search"
               @click="search">搜索</div>
        </div>
      </form>
      <sort-search :typeList="typeList"
                   @item="selectItem"></sort-search>
    </div>
    <div class="goods-list"
         v-show="list.length">
      <!-- <div class="goods-list-title" v-if="is_agent_item">
        <p>下列商品是否上架：</p>
        <div class="btn" :class="[sale == 1 ? 'cur':'']" @click="selectSaleItem(1)">批量上架</div>
        <div class="btn" :class="[sale == 2 ? 'cur':'']" @click="selectSaleItem(0)">批量下架</div>
      </div>-->
      <div class="goods-list-items">
        <van-list v-model="loading"
                  :finished="finished"
                  :error.sync="error"
                  :immediate-check="immediateCheck"
                  error-text="请求失败，点击重新加载"
                  finished-text=""
                  @load="onLoad">
          <div v-for="(item, index) in list"
               :key="index"
               class="list clearfix">
            <div @click="JumpUrl(item.link_item)"
                 class="img fl">
              <img :src="item.pic_url"
                   alt />
              <!-- <span v-if="item.discount && item.discount != 10">{{item.discount}}折</span> -->
            </div>
            <div class="g-flex fl">
              <p @click="JumpUrl(item.link_item)"
                 class="title">
                <span class="fontZiy" v-if="item.is_self_support == 1">自营</span>{{item.title}}
              </p>
              <p class="g-price">
                <span>
                  <em class="sym">{{mSymbol.name}}</em>
                  <em class="int fontGilroy">{{getInt(item.price)}}</em>
                  <em class="dot fontGilroy">{{getDot(item.price)}}</em>
                </span>
                <del v-if="item.is_display_original_price == 1"
                     class="fontGilroy">
                  <em>{{mSymbol.name}}</em>
                  {{item.original_price}}
                </del>
              </p>
              <span class="commission"
                    v-if="item.is_dl_commission == 1">
                预计{{item.commission_name}}：
                <em>{{mSymbol.name}}</em>
                <em class="int fontGilroy">{{item.directly_money}}</em>
              </span>
            </div>
          </div>
        </van-list>
      </div>
    </div>
    <div v-show="!list.length && finished">
      <no-data :noData="noData"></no-data>
    </div>
    <van-popup v-model="showAllClass"
               round
               position="bottom">
      <van-picker show-toolbar
                  :columns="columns"
                  @cancel="onCancel"
                  @confirm="onConfirm" />
    </van-popup>
  </div>
</template>

<script>
import dlsHeader from '@/views/user/dls/components/dlsHeader/index'
import noData from '@/components/noData/index'
import sortSearch from '@/views/item/itemList/listWrapper/sortSearch/index'
import searchBox from '@/views/user/components/search/index'
import { getDlsGoods } from '@/api/user/dls/dlsApi'
import { mapGetters } from 'vuex'
import { openPage } from '@/utils/utils'
import Vue from 'vue'
import { Popup, Picker, List } from 'vant'
import 'vant/lib/index.css'
Vue.use(Popup)
  .use(Picker)
  .use(List)
export default Vue.extend({
  data() {
    return {
      fxsHeaderData: {
        commission_name: '',
        total_commissions: '',

        show_dl_product: false,
        agent_item_count: '',
        agent_item_name: '',

        show_dl_agencies: false,
        agency_total: '',
        ftitle_agencies: '',

        show_dl_agency_user: false,
        agency_user_total: '',
        ftitle_agency_user: '',

        show_dl_agency_order: false,
        agency_order_total: '',
        ftitle_agency_order: ''
      },
      typeList: [
        {
          label: '默认',
          active: false,
          type: 0,
          prop: 'default',
          sort: 'none'
        },
        { label: '价格', active: false, type: 1, prop: 'price', sort: '' },
        {
          label: '销量',
          active: false,
          type: 2,
          prop: 'default',
          sort: 'none'
        },
        { label: '人气', active: false, type: 3, prop: 'default', sort: 'none' }
        // {
        //   label: '',
        //   active: false,
        //   type: 4,
        //   prop: 'default',
        //   sort: 'none'
        // }
      ],
      Placeholder: '请输入商品关键字',
      showBtn: true,
      // 所有分组控制
      showAllClass: false,
      group_list: [],
      columns: [],
      allClassVal: '所有分组',
      allClassId: '-1',
      // 价格区间
      // is_agent_item: null,
      startPrice: '',
      endPrice: '',

      // 列表
      list: [],
      // 排序方式
      search_order: null,
      // 列表vant组件

      loading: false,
      finished: false,
      error: false,
      immediateCheck: false,

      keyWord: '',
      p: 1,
      noData: {
        text: '暂无代理产品',
        img: 'https://img.wifenxiao.com/h5-wfx/images/user/no_distributor.png',
        marginTop: '10em'
      }
    }
  },
  created() {
    // console.log('替换无数据时的图片')
    this.init()
  },
  mounted() {
      // TODO 小程序触底加载更多
      window.addEventListener('reachbottom', () => {
        this.onLoad()
      })
  },
  methods: {
    init() {
      this.$loadingWrap.show()
      this.finished = false
      this.p = 1
      this.list = []
      setTimeout(() => {
        this.$loadingWrap.close()
      }, 50000)
      this.onLoad()
    },
    clickAllClass() {
      this.showAllClass = true
    },
    onCancel() {
      this.showAllClass = false
    },
    onConfirm(val, idx) {
      // console.log(val)
      // console.log(idx)
      if (idx > 0) {
        this.allClassId = this.group_list[idx - 1].group_id
      } else {
        this.allClassId = '-1'
      }
      this.allClassVal = val
      this.showAllClass = false
    },
    allClassList() {
      const list = ['所有分组']
      this.group_list.map(item => {
        list.push(item.title)
      })
      this.columns = list
    },
    onLoad() {
      if (this.finished) return false
      const data = {
        p: this.p,
        search_title: this.keyWord,
        search_group_id: this.allClassId,
        search_start_price: this.startPrice,
        search_end_price: this.endPrice,
        search_order: this.search_order
      }
      getDlsGoods(data).then(res => {
        this.loading = false
        this.$loadingWrap.close()
        if (res.status == 1) {
          if (data.p == 1) {
            // 头部和导航数据
            this.fxsHeaderData.commission_name = res.data.commission_name
            this.fxsHeaderData.total_commissions = res.data.total_commissions
            // 代理产品
            this.fxsHeaderData.show_dl_product = res.data.show_dl_product
            this.fxsHeaderData.agent_item_count = res.data.agent_item_count
            this.fxsHeaderData.agent_item_name = res.data.agent_item_name
            // 代理商
            this.fxsHeaderData.show_dl_agencies = res.data.show_dl_agencies
            this.fxsHeaderData.agency_total = res.data.agency_total
            this.fxsHeaderData.ftitle_agencies = res.data.ftitle_agencies
            // 我的会员
            this.fxsHeaderData.show_dl_agency_user =
              res.data.show_dl_agency_user
            this.fxsHeaderData.agency_user_total = res.data.agency_user_total
            this.fxsHeaderData.ftitle_agency_user = res.data.ftitle_agency_user
            // 团队订单
            this.fxsHeaderData.show_dl_agency_order =
              res.data.show_dl_agency_order
            this.fxsHeaderData.agency_order_total = res.data.agency_order_total
            this.fxsHeaderData.ftitle_agency_order =
              res.data.ftitle_agency_order
            // 搜索部分
            this.group_list = res.data.group_list
            // 处理分组数据
            this.allClassList()
            // 页码为1时   初始列表
            this.list = []
          }
          // 列表
          this.list.push(...res.data.item_list.list)
          if (res.data.item_list.list.length < 10) {
            this.finished = true
          } else {
            this.p++
          }
        } else {
          this.$Error(res.msg)
        }
      })
    },
    getInt(price) {
      if (price) {
        const idx = price.indexOf('.')
        return idx !== -1 ? price.substr(0, idx) : price
      } else {
        return '0'
      }
    },
    getDot(price) {
      if (price) {
        const idx = price.indexOf('.')
        return idx !== -1 ? price.substr(idx) : '.00'
      } else {
        return '.00'
      }
    },
    search() {
      this.p = 1
      this.finished = false
      this.error = false
      this.loading = false
      this.$loadingWrap.show()
      setTimeout(() => {
        this.$loadingWrap.close()
      }, 10000)

      this.onLoad()
    },
    // 排序方式
    selectItem({ sort, prop, type }) {
      if (type == 0) {
        // 默认
        this.search_order = null
      } else if (type == 1) {
        if (sort == 'ascending') {
          // 价格升
          this.search_order = 'up'
        } else if (sort == 'descending') {
          // 价格降
          this.search_order = 'down'
        }
      } else if (type == 2) {
        if (sort == 'ascending') {
          // 销量升
          this.search_order = 'sales_volume'
        } else if (sort == 'descending') {
          // 销量降   后台暂时没有接口
          // this.search_order = 'sales_volume'
        } else {
          this.search_order = 'sales_volume'
        }
      } else if (type == 3) {
        if (sort == 'ascending') {
          // 人气升
          this.search_order = 'pv'
        } else if (sort == 'descending') {
          // 人气降 后台暂时没有接口
          // this.search_order = 'pv'
        } else {
          this.search_order = 'pv'
        }
      }
      this.search()
    },
    JumpUrl(url) {
      // TODO 小程序跳转处理
        openPage(url)
    }
  },
  components: {
    dlsHeader,
    noData,
    sortSearch,
    searchBox
  },
  computed: {
    // 获取自定义符号
    ...mapGetters(['mSymbol'])
  }
})
</script>

<style lang="scss">
@import "../../../../styles/variables";
@import "../../../../styles/mixin";
.fenxiao-goods {
  .item-card {
    margin-top: 20px;
    padding: 10px 25px;
    overflow: hidden;
    background-color: $default;
    .select-item {
      padding: 10px 0;
      display: flex;
      align-items: center;
      justify-content: space-between;
      flex-wrap: nowrap;
      div {
        // flex: 1;
        &.all-class {
          display: flex;
          justify-content: center;
          align-items: center;
          font-size: 24px;
          color: #666;
          width: 190px;
          height: 66px;
          text-align: center;
          padding: 0 10px 0 20px;
          border: 2px solid #ededed;
          border-radius: 5px;
          .text {
            display: inline-block;
            max-width: 145px;
            font-size: 24px;
            @include showEllipsis;
          }
          .icon-right {
            margin-left:8px;
            margin-bottom: 3px;
            transform: rotateZ(45deg);
          }
        }
        &.text {
          width: 120px;
          font-size: 24px;
          color: #999;
          white-space: nowrap;
        }
        &.search {
          width: 124px;
          line-height: 66px;
          background: #f30c23;
          border-radius: 5px;
          font-size: 24px;
          text-align: center;
          color: #fff;
        }
        &.price {
          @extend .all-class;
          width: 160px;
          padding: 0;
          input {
            width: 100%;
            height: 66px;
            border: 2px solid #ededed;
            border-radius: 5px;
            border: none;
            background-color: transparent;
            padding: 20px;
            color: #999;
            &::placeholder {
              text-align: center;
              color: #999;
              // line-height: 1.4;
            }
          }
          &:last-child {
            margin-left: 20px;
            position: relative;
            &::after {
              content: "";
              position: absolute;
              left: -17px;
              top: 50%;
              transform: translateY(-50%);
              width: 10px;
              height: 2px;
              background-color: #666;
            }
          }
        }
      }
    }
  }
  .goods-list {
    background-color: $default;
    &-title {
      display: flex;
      align-items: center;
      justify-content: flex-start;
      font-size: 26px;
      color: #333;
      .btn {
        width: 150px;
        height: 50px;
        padding-top: 10px;
        // line-height: 52px;
        text-align: center;
        border-radius: 24px;
        background-color: $default;
        border: 2px solid #e8e8e8;
        color: #666;
        font-size: 24px;
        box-sizing: border-box;
        margin-right: 15px;
        span {
          display: flex;
          justify-content: center;
          align-items: center;
        }
        &.cur {
          border: 2px solid #ffe0cf;
          background: #ffefe6;
          color: #ff5901;
        }
      }
    }
    &-tab {
      display: flex;
      justify-content: space-between;
      align-items: center;
      span {
        // flex: 1;
        text-align: center;
      }
    }
    &-items {
      .list {        
        position: relative;
        font-family: PingFang SC;
        padding: 20px;
        border-bottom: 1px solid #ededed;
        &::before {
          width: 230px;
          content: "";
          position: absolute;
          left: 0;
          height: 5px;
          bottom: -1px;
          background: #fff;
        }
        .img {
          background-color: #f5f5f5;
          display: inline-block;
          width: 190px;
          height: 190px;
          border-radius: 12px;
          overflow: hidden;
          position: relative;
          img {
            width: 100%;
            height: 100%;
          }
          span {
            display: inline-block;
            overflow: hidden;
            position: absolute;
            top: 0px;
            left: 0px;
            color: #fff;
            background: #e4393c;
            padding: 0 4px;
          }
        }
        .g-flex {
          // margin: 0;
          padding-left: 20px;
          width: 485px;
          height: 190px;
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          .title {
            @include lineClamp();
            font-size: 24px;
            color: #333;
            font-weight: bold;
          }
        }
        .g-price {
          display: inline-block;
          margin: 0;
          margin-top: 44px;
          span {
            color: $warning;
            font-size: 24px;
            em {
              font-weight: bold;
            }
            .int {
              font-size: 32px;
              margin-right: -5px;
            }
          }
          del {
            margin-left: 22px;
            color: #999;
            margin-left: 5px;
          }
        }
        .commission {
          font-size: 22px;
          color: #999999;
          em {
            color: #ff8c01;
            margin-left: -5px;
            &.int {
              font-size: 26px;
              margin-left: -5px;
            }
          }
        }
        .g-fx-btn {
          margin-top: 10px;
          span {
            display: block;
            float: left;
            width: 130px;
            height: 50px;
            margin-right: 20px;
            line-height: 52px;
            text-align: center;
            border-radius: 25px;
            overflow: hidden;
            a {
              display: block;
              width: 100%;
              height: 100%;
              color: $default;
              font-size: 24px;
              font-weight: normal;
              color: $default;
              background-color: $warning;
              &.not {
                background-color: #f5f5f5;
                border-color: red;
                color: #666;
              }
            }
          }
        }
      }
    }
  }
}
</style>
